//
// Created by dave on 18-7-29.
//

#pragma once
#include <vector>
#include <string>
#include <cstdlib>
#include <ctime>
#include <memory>
#include <algorithm>

class DataPack;

unsigned short read_word(std::vector<char>& data, std::size_t start);

unsigned int read_dword(std::vector<char>& data, std::size_t start);

std::string read_bcd(std::vector<char>& data, std::size_t start, std::size_t size);

void write_word(std::vector<char>& data, unsigned short word);

void write_dword(std::vector<char>& data, unsigned int dword);

void write_bytes(std::vector<char>& data, const char* ptr, size_t length);

std::vector<char> get_bcd_bytes(const std::string& data);

void write_bcd(std::vector<char>& buf, const std::string& data);

char checksum(std::vector<char>& buf);

DataPack decode_data_pack(const char* buf, std::size_t start, std::size_t stop);

std::string gbk2utf8(const std::string& str);

std::string utf8_gbk(const std::string& str);

time_t parse_time(const std::string& time);

std::string format_time(time_t t);

template<typename ... Args>
std::string string_format( const std::string& format, Args ... args ) {
    int size = snprintf( nullptr, 0, format.c_str(), args ... ) + 1; // Extra space for '\0'
    std::unique_ptr<char[]> buf( new char[ size ] );
    snprintf( buf.get(), static_cast<size_t>(size), format.c_str(), args ... );
    return std::string( buf.get(), buf.get() + size - 1 ); // We don't want the '\0' inside
}